/*	
**    Copyright (C) 1992  Ronin Consulting, Inc.
**
**    This program is free software; you can redistribute it and/or modify
**    it under the terms of the GNU General Public License as published by
**    the Free Software Foundation; version 1.
**
**    This program is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**    GNU General Public License for more details.
**
**    You should have received a copy of the GNU General Public License
**    along with this program; if not, write to the Free Software
**    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#import "GISOPrefPanel.h"
#import "Defaults.h"
#import "StringStorage.h"
#import <appkit/Button.h>
#import <appkit/publicWraps.h>
#import <libc.h>
#import <string.h>
#include <sys/types.h>
#include <sys/stat.h>


static NXDefaultsVector appDefs =  
{
{ "StoreDirectory", "./English.lproj" },
{ "TmpDirectory", "/tmp"},
{NULL}
};

@implementation GISOPrefPanel

- init
{
   [super init];

   [defaults regDefaults: appDefs];
   return self;
}

- save: sender
{
   struct stat buf;
   char *ndir;
   id buffer = [[StringStorage alloc] init];

   if([[sender window] makeFirstResponder: [sender window]])
       [[sender window] endEditingFor: nil];
   else
   {
      NXBeep();
      return self;
   }
   
   ndir = (char *)[storeDirectory stringValue];
   
   if(ndir && *ndir)
   {
   
      if(stat(ndir, &buf) || !(buf.st_mode & S_IFDIR))
      {
	 NXBeep();
	 return self;
      }

      [defaults writeDB: "StoreDirectory" as: ndir];
   
   
      [buffer setStringValue: "cp English.lproj/inHotList.strings "];
      [buffer appendStringValue: ndir];
      [buffer appendStringValue: "/inHotList.strings"];
      system([buffer stringValue]);
      [buffer setStringValue: "cp English.lproj/inHotListConfig.strings "];
      [buffer appendStringValue: ndir];
      [buffer appendStringValue: "/inHotListConfig.strings"];
      system([buffer stringValue]);
      [buffer setStringValue: "cp English.lproj/outHotList.strings "];
      [buffer appendStringValue: ndir];
      [buffer appendStringValue: "/outHotList.strings"];
      system([buffer stringValue]);
      [buffer setStringValue: "cp English.lproj/outHotListConfig.strings "];
      [buffer appendStringValue: ndir];
      [buffer appendStringValue: "/outHotListConfig.strings"];
      system([buffer stringValue]);
   }
   

   ndir = (char *)[tmpDirectory stringValue];
   
   if(ndir && *ndir)
   {
      if(stat(ndir, &buf) || !(buf.st_mode & S_IFDIR))
      {
	 NXBeep();
	 return self;
      }

      [defaults writeDB: "TmpDirectory" as: ndir];
   }
   
   return [[sender window] performClose: sender];
}

- windowDidBecomeKey:sender
{
   [storeDirectory setStringValue: [defaults get: "StoreDirectory"]];
   [tmpDirectory setStringValue: [defaults get: "TmpDirectory"]];
   return self;
}
@end
